<template>
<el-container class="home-container">
  <el-header>
    <div>
      <img src="../assets/heima.png" alt="" />
      <span>电商后台管理系统</span>
    </div>
    <el-button type="info" @click="logout">退出</el-button>
  </el-header>
  <el-container>
    <el-aside :width="flag ? '64px' : '200px'">
      <el-menu background-color="#333744" text-color="#fff" active-text-color="skyblue" unique-opened :collapse="flag" :collapse-transition="false" router :default-active="activelinks">
        <div class="toggle" @click="handletoggle"><span>|||</span></div>
        <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
          <template slot="title">
            <i :class="iconobj[item.id]"></i>
            <span>{{ item.authName }}</span>
          </template>
          <el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveactivelinks('/' + subitem.path)">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{ subitem.authName }}</span>
            </template></el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      flag: false,
      activelinks: '',
      iconobj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      }
    }
  },
  created() {
    this.getMenuList(),
      this.activelinks = sessionStorage.getItem('activelinks')
  },

  methods: {
    logout() {
      sessionStorage.removeItem('token')
      this.$message.success('退出成功')
      this.$router.push('/login')
    },
    async getMenuList() {
      const {
        data: res
      } = await this.$http.get('menus')
      console.log(res)
      this.menuList = res.data
    },
    handletoggle() {
      this.flag = !this.flag
    },
    saveactivelinks(activelinks) {
      sessionStorage.setItem('activelinks', activelinks)
      this.activelinks = activelinks
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
    height: 100%;
}
.el-header {
    background-color: #373d41;
    padding-left: 0;
    display: flex;
    justify-content: space-between;
    div {
        display: flex;
        align-items: center;
        span {
            font-size: 20px;
            color: #fff;
            padding-left: 20px;
        }
    }

    button {
        align-self: center;
    }
}
.el-aside {
    background-color: #333744;
}
.el-main {
    background-color: #eaedf1;
}
.el-menu {
    border-right: none;
    .el-submenu {
        span {
            margin-left: 10px;
        }
    }
}
.toggle {
    background-color: #373d41;
    line-height: 30px;
    height: 30px;
    color: #fff;
    text-align: center;
    cursor: pointer;
    letter-spacing: 0.2em;
}
</style>
